﻿using UnityEngine;
using System.Collections;

public class CandyMovement : MonoBehaviour {
    public Vector3 from_pos, target_pos;
    public float start_time;
    private GameManager manager;
   public static CandyMovement instance;
	void Start () {
        instance = this;
        manager = GameManager.getInstance();
    }

    public void Moving()
	{ 
		Swap(CandyNeighbor.instance.clicked_candy_,CandyNeighbor.instance.target_candy);
        Invoke("DropCandy",0.55f);
		manager.gamestate = GameState.checking;
        CandyCheckMatch.instance.Invoke("Check", 0.7f);

    }
    private void DropCandy()
    {
        CandyDrop.instance.KinematicCandy(false);
        
    }
    public void SwapCandy(GameObject obj)
	{      
		if((manager.gamestate!=GameState.moving)||(manager.IsDestroySpecialMatch)){return;}
        CandyNeighbor.instance.clicked_candy_ = obj;
		CandyNeighbor.instance.FindNearByToMove();
    }
   
    private void Swap(GameObject obj, GameObject target_obj)
    {
        if ((obj == null) || (target_obj == null)) { return; }
        CandyNeighbor.instance.from_pos = obj.gameObject.transform.position;
        CandyNeighbor.instance.target_pos = target_obj.gameObject.transform.position;

        iTween.MoveTo(obj.gameObject, CandyNeighbor.instance.target_pos, .5f);
        iTween.MoveTo(target_obj.gameObject, CandyNeighbor.instance.from_pos , .5f);
        SoundCon.instance.PlaySound(2);
    }
    private float swap_time;
    public void MoveBack()
    {
        Swap(CandyNeighbor.instance.target_candy, CandyNeighbor.instance.clicked_candy_);
        Invoke("DropCandy", 0.55f);
		manager.gamestate=GameState.checkboardstate;
		CandyCheckMatch.instance.Invoke("CheckBoardState", 0.7f);
		SoundCon.instance.PlaySound(10);
    }
}
